<?php

use Biz\cache\brand\BlindBox as cBlindBox;
use Biz\model\brandActivity\BlindBoxPointTask as mBlindBoxPointTask;
use Biz\model\ShopFavorite as mShopFavorite;
use Biz\service\brand\BlindBox;
use Biz\service\ShopGoodsOperator;
use Biz\service\wiki\WikiBasic;
use Doraemon\tools\PointConfig;
use hhz\constant\redis\redisKeys;
use Hhz\Redis\otherRedis;

/**
 * 收藏wiki
 */
class FavWikiAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        try {
            $basic_id = $this->getRequest()->getPost('basic_id', 0);
            $action_from = $this->getRequest()->getPost('action_from', 'blind_box_fav');
            if (!$basic_id) {
                throw new \UnexpectedValueException('参数错误');
            }
            $key = $this->uid . '_brand_blindbox_favwiki_acttion';
            //5秒禁止提交
            $lock = \Biz\cache\brand\BrandShowActivity::isLock($key, 1);
            if ($lock) {
                throw new \UnexpectedValueException('请不要频繁提交');
            }
            $basic_ids = explode(',', $basic_id);

            $taskInfo = mBlindBoxPointTask::selectByWhere([
                'uid' => $this->uid,
                'obj_id' => $basic_id,
                'task_type' => mBlindBoxPointTask::TASK_TYPE_FAVORITE_GOODS
            ], ['obj_id']);
            $obj_ids = [];
            if ($taskInfo) {
                $obj_ids = array_column($taskInfo, 'obj_id');
            }
            //获取为加分的id
            $new_ids = array_diff($basic_ids, $obj_ids);

            if ($new_ids) {
                $basicWikiInfos = WikiBasic::getWikiBasicInfoByIds($new_ids);

                $wiki_ids = array_column($basicWikiInfos, 'spu_id') ?? [];
                //收藏
                foreach ($wiki_ids as $wiki_id) {
                    if (!$wiki_id) {
                        continue;
                    }
                    try {
                        ShopGoodsOperator::favorite($this->uid, $wiki_id, mShopFavorite::TYPE_WIKI);
                    } catch (\Exception $e) {
                        \Doraemon\tools\Log::debug($e->getMessage(), ["wiki_id" => $wiki_id, "uid" => $this->uid]);
                    }
                    cBlindBox::todayFavWiki($this->uid);

//                    $constKey = redisKeys::KEY_B_OTHER_BLIND_BOX_TODY_FAV_WIKI_;
//                    $redis = new otherRedis($constKey);
//                    $key = $constKey . $this->uid;
//                    $redis->incr($key);
//                    $expireTime = strtotime(date('Y-m-d')) + 86400 - time();
//                    $redis->expire($key,$expireTime);
                }


                $params['uid'] = $this->uid;
                $params['new_ids'] = $new_ids;
                $params['point'] = BlindBox::TASK_FAVORITE_GOODS_POINT;
                $params['event_name'] = PointConfig::POINT_TYPE_BLIND_BOX_FAVORITE_WIKI;
                $params['task_type'] = mBlindBoxPointTask::TASK_TYPE_FAVORITE_GOODS;
                $params['task_state'] = mBlindBoxPointTask::TASK_STATE_FINISHED;
                $params['type'] = 2;
                //加分操作
                $result = BlindBox::addScore($params);

                $msg = '';
                if ($result) {
                    $msg = '收藏成功';
                }
            }
        } catch (UnexpectedValueException $e) {
            return $this->setSuccess(['msg' => $e->getMessage()], 0);
        } catch (Exception $e) {
            \Doraemon\tools\Log::error('品牌盲盒收藏失败-error:' . $e->getMessage(), $this->getRequest()->getPost());
            return $this->setSuccess(['msg' => '系统错误: ' . $e->getMessage()], 0);
        }
        \Doraemon\tools\ActionLog::log($this->uid, ['uid' => $this->uid, 'action_from' => $action_from], \Doraemon\tools\ActionLog::BLIND_BOX_FAV);
        $this->setSuccess(['msg' => $msg]);
    }

}